#include "stdafx.h"
#include "HXMorph.h"

//-----------------------------------------------------------------------------

//! Morph labels for the default 3D models loaded by Head X
/*const char* HXMorph::morphLabels[HXMorph::morphLabelCount] = {"Expression: Anger", "Expression: Disgust", "Expression: Fear", "Expression: Sad",
"Expression: SmileClosed", "Expression: SmileOpen", "Expression: Surprise", "Modifier: Blink Left", "Modifier: Blink Right", 
"Modifier: BrowDown Left", "Modifier: BrowDown Right", "Modifier: BrowIn Left", "Modifier: BrowIn Right", "Modifier: BrowUp Left", 
"Modifier: BrowUp Right", "Modifier: Ears Out", "Modifier: Epicanthic Fold", "Modifier: EyeSquint Left", "Modifier: EyeSquint Right", 
"Modifier: LookDown", "Modifier: LookLeft", "Modifier: LookRight", "Modifier: LookUp", "Phoneme: aah", "Phoneme: B,M,P", "Phoneme: big aah", 
"Phoneme: ch,J,sh", "Phoneme: D,S,T", "Phoneme: ee", "Phoneme: eh", "Phoneme: F,V", "Phoneme: i", "Phoneme: K", "Phoneme: N", "Phoneme: oh", 
"Phoneme: ooh,Q", "Phoneme: R", "Phoneme: th", "Phoneme: W"};*/

//-----------------------------------------------------------------------------

//! Constructor
HXMorph::HXMorph() 
: morphAdjustableCount(0),
  dummyVal(0.0f)
{
}

//! Retrieve available morphs from model.
void HXMorph::CreateFrom(HXModel& model)
{
	statMorphVals.resize(model.statMorphList.size(), 0.0f);
	diffMorphVals.resize(model.diffMorphList.size(), 0.0f);
	morphMap.clear();
	for (size_t i = 0; i < model.statMorphList.size(); i++)
	{
		morphMap[model.statMorphList[i].wlabel].Set(i, HXMT_STAT);
	}
	for (size_t j = 0; j < model.diffMorphList.size(); j++)
	{
		morphMap[model.diffMorphList[j].wlabel].Set(j, HXMT_DIFF);
	}
	morphAdjustableCount = model.statMorphList.size() + model.diffMorphList.size();
}

//! Set all morph values to zero.
void HXMorph::Reset()
{
	vector<float>::iterator flit;
	for (flit = diffMorphVals.begin(); flit != diffMorphVals.end(); ++flit)
	{
		*flit = 0.0f;
	}
	for (flit = statMorphVals.begin(); flit != statMorphVals.end(); ++flit)
	{
		*flit = 0.0f;
	}
}

//! Retrieve the value of a morph by its label.
float& HXMorph::operator[](const wstring& label) 
{ 
	HXMorphIndex& target = morphMap[label];
	switch (target.source)
	{
	case HXMT_DIFF:
		return diffMorphVals[target.index];
	case HXMT_STAT:
		return statMorphVals[target.index];
	default:
		return dummyVal;
	}
	
}

//-----------------------------------------------------------------------------